import { Component, OnInit, NgModule} from '@angular/core';
import { Command } from "../../../../../shared/toolbar.service";
import { FormBuilder, FormControl, Validators, FormsModule } from "@angular/forms";
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { Menu } from "../../../../../entity/menu.model";
import { MenuAccessService } from "../../../../../service/menu_access.service";
import { MenuAccess } from "../../../../../entity/menu_access.model";
// import { NgModule } from "@angular/core";
// import {FormsModule} from "@angular/forms"

@Component({
  selector: 'app-menu-access-edit',
  templateUrl: './menu-access-edit.component.html',
  styleUrls: ['./menu-access-edit.component.css']
})
export class MenuAccessEditComponent extends BladeContainerBase<Menu> {
  isShowMenuShortCuts: boolean = false;
  disable: boolean;

  constructor(
    private fb: FormBuilder,
    private menuAccessService: MenuAccessService,
  ) {
    super(fb);
  }

  ngOnInit() {
    if (!this.isNew) {
      this.commands = [
        new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
        new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
      ]
    }
    super.ngOnInit();
  }

  //保存
  exeSaveChange(blade: any): void {
    this.saveChange();
  }
  canSaveChange(blade: any): boolean {
    // console.log(this.hasChange(),!this.hasError())
    return this.hasChange() && !this.hasError();
  }
  //重置
  exeReset(blade: any): void {
    return this.formGroup.reset(this.currentEntity);
  }
  canReset(blade: any): boolean {
    return this.hasChange();
  }
 
  //创建表单
  createForm() {
    this.isShowMenuShortCuts = (this.currentEntity.parentId == 0) ? false : true;
    this.disable = (this.currentEntity.isShow == true) ? false : true;
    this.formGroup = this.fromBuider.group({
      "menuName": [this.currentEntity.menuName, [
        Validators.required,
        Validators.maxLength(100),
      ],],
      "isShow": [this.currentEntity.isShow, [],],      
      "unMenuShortCuts":[this.currentEntity.unMenuShortCuts, []] //  [{value:this.currentEntity.unMenuShortCuts, disabled: this.disable}, []] 
    });

    this.formGroup.controls["isShow"].valueChanges.subscribe(value => {
		  if(value == false) {
        this.disable=true;
      }else{
        this.disable=false;
      }
    })
  }
  
  setFormError(): void {
    this.formErrors = {
      'menuName': '',
      'unMenuShortCuts': '',
      'isShow': '',
    };
  }

  setValidationMessages(): void {
    this.validationMessages = {
      'menuName': {
        'required': '菜单名称不能为空。',
        'maxlength': '菜单名称不能多于100个字符长度。',
      },
      'unMenuShortCuts': {},
      'isShow': {},

    };
  }

  saveChange(): Promise<boolean> {

    if (this.canSaveChange(this.currentBlade)) {

      this.bladeData.isLoading = true;
      let formValue = this.formGroup.value as Menu;

      let unMenuShortCuts = false;
      unMenuShortCuts = formValue.isShow ? formValue.unMenuShortCuts : false;


      if (!this.isShowMenuShortCuts) {//第一级菜单
       
        let menuIds:number[] = [] 
        this.currentEntity.children.forEach(item =>{
          menuIds.push(item.menuId);      
        })
        menuIds.push(this.currentEntity.menuId);
        let currentMenuAccess: MenuAccess = {
          roleId: this.parentEntity.roleId,
          menuId: this.currentEntity.menuId,
          unVisible: formValue.isShow,
          menuIds: menuIds,
        }
        // console.log(currentMenuAccess)
        
        return this.menuAccessService.firstCreate(currentMenuAccess)
          .then(res => {
            if (res != -1) {
              // // currentMenuAccess.unVisible = false
              // console.log(this.currentEntity)
              let parentContainerInstance = this.parentBlade.bladeContainerInstance;
              parentContainerInstance.refresh(res);
              this.bladeService.closeBlade(this.currentBlade);              
              return true;
            }
          })
          .catch(error => this.bladeData.error = error)
          .then(res => this.bladeData.isLoading = false);
      } else {//第二级菜单
        let currentMenuAccess: MenuAccess = {
          roleId: this.parentEntity.roleId,
          menuId: this.currentEntity.menuId,
          unVisible: formValue.isShow,//是否可见
          unMenuShortCuts: unMenuShortCuts//固定菜单
        }
        // console.log(currentMenuAccess)
        return this.menuAccessService.create(currentMenuAccess)
          .then(res => {
            if (res != -1) {
              let parentContainerInstance = this.parentBlade.bladeContainerInstance;
              parentContainerInstance.refresh(res);
              this.bladeService.closeBlade(this.currentBlade);
              return true;
            }
          })
          .catch(error => this.bladeData.error = error)
          .then(res => this.bladeData.isLoading = false);
      }

    }
  }

  
}




// WEBPACK FOOTER //
// ./src/app/module/admin/role/menu-access/menu-access-edit/menu-access-edit.component.ts